// distribution/goodlist/goodlist.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';



Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            // "homeflag": true,
            "name": "分销商品"
        },

        good_list: [],
        noMore: false,

        refreshing: false
    },

    page: 1,
    page_size: 5,

    // 获取分销商品
    async getShareGoods() {
        let params = {
            page: this.page,
            limit: this.page_size,
            // type: this.data.type,
            brand_id: 2
        }
        let list = this.data.good_list
        let res = await request.post(api.goods_list, params)
        if (res.length == 0) {
            this.setData({
                noMore: true
            })
            util.showToast('暂无更多相关商品')
            return
        }

        // 计算最高可赚佣金
        res.forEach(item => {
            item.highestCommision = (item.sell_price * 0.1).toFixed(2)
        });

        let new_list = list.concat(res)
        this.setData({
            good_list: new_list
        })
    },

    init_data(options) {
        this.uid = App.globalData.uid

        // this.setData({
        //     uid: App.globalData.uid,
        //     userInfo: App.globalData.userInfo,
        //     avatarUrl: App.globalData?.fulluserInfo?.avatar || defaultAvatarUrl,
        //     nickname: App.globalData?.fulluserInfo?.name|| '游客',
        //     mobile: App.globalData.mobile,
        //     options,
        // })
    },

    // 下拉刷新列表
    async pullRefresh() {
        this.page = 1
        this.page_size = 5

        console.log(99999);
        util.showLoading('加载中')

        this.setData({
            good_list: [],
            noMore: false
        })

        await this.getShareGoods()

        // 停止刷新
        this.setData({
            refreshing: false
        })
        util.hideLoading()
    },

    // 上拉加载更多 必须用fixed定位确定展示区域，才能拖动加载
    loadMoreGoods() {
        console.log(556);
        let nomre = this.data.noMore
        if (nomre) return
        this.page++
        this.getShareGoods()
    },

    // 点击分享
    tapToShare(e) {
        let id = e.currentTarget.dataset.id
        console.log('tapToShare', id);
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.init_data()
        this.getShareGoods()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        let mobile = App.globalData.mobile
        let VipGrade = this.data.VipGrade
        let uid = this.data.uid
        let id = e.target.dataset.id
        let title = e.target.dataset.title
        let imgurl = e.target.dataset.imgurl

        let sid = `${uid}a${id}a1`

        // 记录分享次数
        request.postall(api.wxacodeshare, { uid: this.data.uid, sid: sid }).then((res) => {
            console.log(res);
        })

        let nobind = 1 // 正常不绑定关系
        if (mobile.length == 11 && VipGrade >= 1) {
            nobind = 0 // 如果是分销员 则置零 绑定关系
        }

        let share_path = `/pages/good/detail`;
        let sharePath = `/pages/home/home?share=${share_path}&sid=${sid}&id=${this.data.goods_id}&nobind=${nobind}`

        // console.log(share_path, sharePath);
        let shareObj = {
            title: title,
            path: sharePath,
            // imageUrl: this.data.detail_data.img,
            imageUrl: imgurl,

            success: function (res) {
                if (res.errMsg == 'shareAppMessage:ok') {
                    util.showToast('分享成功');
                }
            },
            fail: function (err) {
                console.log(err);
                util.showToast('分享失败');
            },
        };
        console.log('shareObj', shareObj);

        return shareObj;
    }
})